//jwt.strategy.ts
import { ConfigService } from "@nestjs/config";
import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { StrategyOptions, Strategy, ExtractJwt } from "passport-jwt";
import { Users } from "../users/entities/user.entity";

// 定义一个JwtStorage类，继承自PassportStrategy
@Injectable()
export class JwtStorage extends PassportStrategy(Strategy, "jwt") {
	// 构造函数，注入UsersRepository、ConfigService、AuthService和CacheManager
	constructor(private readonly configService: ConfigService) {
		// 调用父类的构造函数，传入jwtFromRequest和secretOrKey
		super({
			jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
			secretOrKey: configService.get("JWT_SHORT_SECRET")
		} as StrategyOptions);
	}

	// 验证token的方法
	async validate(user: Users) {
		console.log("jwt验证成功");

		// 返回用户信息
		return user;
	}
}

// 定义RefreshJwtStrategy类，继承自PassportStrategy
@Injectable()
export class RefreshJwtStrategy extends PassportStrategy(Strategy, "refreshJwt") {
	constructor(private readonly configService: ConfigService) {
		// 调用父类的构造函数，传入jwtFromRequest和secretOrKey
		super({
			jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
			secretOrKey: configService.get("JWT_LONG_SECRET")
		} as StrategyOptions);
	}

	// 验证函数，传入user参数
	async validate(user: Users) {
		// 返回existUser
		return user;
	}
}
